package com.example.springboot004extensionpoint.beanPostProcessor;

import com.example.springboot004extensionpoint.beanFactoryPostProcessor.Person;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class KoalaBeanPostProcessor implements BeanPostProcessor {
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException
    {
        if(StringUtils.pathEquals(beanName, "person"))
        {
            System.out.println("我是在 postProcessBeforeInitialization 获取的Person："+ ((Person) bean).getName());
        }
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException
    {
        if(StringUtils.pathEquals(beanName, "person"))
        {
            System.out.println("我是在 postProcessAfterInitialization 执行变更前 获取的Person："+ ((Person) bean).getName());
            Person person = (Person)bean;
            person.setName("我是在 postProcess变更了");
            System.out.println("我是在 postProcessAfterInitialization 执行变更后 获取的Person："+ ((Person) bean).getName());
        }
        return bean;
    }
}
